package com.gt.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 字符串加工工具类
 */
@Slf4j
public class XyStringUtils {

    /**
     * 字符串截取
     * @param old 字符串
     * @param length 截取的长度
     * @return
     */
    public static String cutting(String old,int length){
        if (StringUtils.isEmpty(old)){
            return old;
        }
        return old.length()<=length?old:old.substring(0,length);
    }

    /**
     * 获取异常堆栈信息
     * @param t
     * @return
     */
    public static String getStackTrace(Throwable t) {
        try {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            try {
                t.printStackTrace(pw);
                return sw.toString();
            } finally {
                pw.close();
            }
        }catch (Exception e){
            log.error("com.xynew.utils.XyStringUtils.getStackTrace 方法执行异常",e);
        }
        return null;
    }

}
